<template>
<el-container>
  <el-header>
    <el-form>
      <el-row>
        <el-col :xl="{ span: 4 }">
          <el-form-item>
            <el-button type="primary" size="small" @click="addRegulation">添加规则</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </el-header>
  <el-main>
    <el-table :header-cell-style="{ 'background':$store.state.app.tableBgColor,'color':'#606266' }" :data="tableData" tooltip-effect="dark" style="width: 100%" @selection-change="handleSelectionChange" v-loading.fullscreen="tableLoading" element-loading-text="拼命加载中" element-loading-spinner="el-icon-loading" element-loading-background="rgba(0, 0, 0, 0.2)">
      <el-table-column type="selection" align="center"></el-table-column>
      <el-table-column prop="name" label="规则名字" align="center"></el-table-column>
      <el-table-column prop="pre" label="首字段" align="center"></el-table-column>
      <el-table-column prop="incrementNum" label="自增字段" align="center"></el-table-column>
      <el-table-column prop="currentNum" label="当前规则" align="center"></el-table-column>
    </el-table>
    <div class="block"><span class="demonstration"></span>
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="currentPage" :page-size="10" layout="prev, pager, next, jumper" :page-count="maxPage" :hide-on-single-page="value" background></el-pagination>
    </div>
  </el-main>
  <el-dialog class="regulation" title="卡片详情" top="10vh" :visible.sync="regulation" :append-to-body="true" width="30%" :before-close="handleClose">
    <el-form ref="form" :model="form" label-width="80px" :rules="formRules">
      <el-form-item label="规则名称" prop="name">
        <el-input v-model="form.name" size="small"></el-input>
      </el-form-item>
      <el-form-item label="首字段" prop="pre">
        <el-input v-model="form.pre" size="small"></el-input>
      </el-form-item>
      <el-form-item label="自增字段" prop="incrementNum">
        <el-input v-model="form.incrementNum" size="small"></el-input>
      </el-form-item>
      <el-form-item label="示例" prop="currentNum">
        <el-input v-model="currentNum" size="small"></el-input>
      </el-form-item>
    </el-form><span class="dialog-footer" slot="footer">
      <el-button @click="submitAddRegulation" size="small" type="primary">确定</el-button></span>
  </el-dialog>
</el-container></template>

<script>
import { regulationList, addRegulation } from '@/api/getData'

export default {
  name: 'RegulationList',
  data() {
    const validateCurrentNum = (rule, value, callback) => {
      if (this.currentNum.length > 40) {
        callback(new Error('首字段+自增字段不能超过40位'))
      } else {
        callback()
      }
    }
    return {
      formRules: {
        name: [{ required: true, message: '请输入规则名称', trigger: 'blur' }],
        pre: [
          { required: true, message: '请输入首字段', trigger: 'blur' },
          { pattern: /^[A-Za-z]+$/, message: '首字段必须是字母', trigger: 'blur' }
        ],
        incrementNum: [
          { required: true, message: '请输入自增字段', trigger: 'blur' },
          { pattern: /^[1-9]+[0-9]*$/, message: '自增字段必须是正整数', trigger: 'blur' }
        ],
        currentNum: [
          { validator: validateCurrentNum }
        ]
      },
      tip: '',
      form: {
        name: '',
        pre: '',
        incrementNum: ''
      },
      pageSize: 10,
      currentPage: 1,
      maxPage: 1,
      ascs: [],
      descs: [],
      multipleSelection: [],
      tableData: [],
      value: false,
      tableLoading: false,
      id: '',
      regulation: false,
      name: '',
      pre: '',
      incrementNum: ''
    }
  },
  computed: {
    currentNum() {
      return this.form.pre + this.form.incrementNum
    }
  },
  created() {
    this.initData()
  },
  methods: {
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    handleSizeChange(val) {
      //console.log(`每页 ${val} 条`)
      this.pageSize = val
    },
    handleCurrentChange(val) {
      //console.log(`当前页: ${val}`)
      this.currentPage = val
      this.initData()
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => {})
    },
    addRegulation() {
      this.regulation = true
    },
    async initData() {
      const returnData = await regulationList({
        page: {
          current: this.currentPage + '',
          size: this.pageSize + '',
          ascs: this.ascs,
          descs: this.descs
        }
      },
      {
        obj: this,
        key: 'tableLoading'
      })
      this.tableData = []
      if (returnData._result) {
        this.maxPage = Math.ceil(returnData.data.total / returnData.data.pageSize)
        returnData.data.records.map(item => {
          const obj = {}
          obj.name = item.name
          obj.pre = item.pre
          obj.incrementNum = item.incrementNum
          obj.currentNum = item.pre + item.currentNum
          this.tableData.push(obj)
        })
        this.tableLoading = false
      }
    },
    async submitAddRegulation() {
      const returnData = await addRegulation({
        name: this.form.name,
        pre: this.form.pre,
        incrementNum: this.form.incrementNum,
        currentNum: this.form.incrementNum
      })
      if (returnData._result) {
        this.regulation = false
        this.$message({ message: '添加成功', type: 'success' })
        this.initData()
      }
    }
  }
}
</script>

<style scoped lang="scss">
.el-container {
  .el-header {
    margin-top: 5vh;
  }

  .el-main {
    .block {
      float: right;
    }
    .el-dropdown-link {
      font-size: 20px;
      cursor: pointer;
    }
  }
}
.regulation {
  margin-top: 15vh;
}
</style>
